package com.jelly.web.modules.system.role.service.impl;

import com.jelly.common.util.StringUtils;
import com.jelly.web.modules.system.role.dao.RolePermissionMapper;
import com.jelly.web.modules.system.role.domain.RolePermission;
import com.jelly.web.modules.system.role.domain.RolePermissionExample;
import com.jelly.web.modules.system.role.service.IRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @创建人 ql
 * @创建时间 2018-09-14 15:00
 * @项目名称 jelly
 * @功能描述: 角色权限实现类
 **/
@Service
public class RolePermissionServiceImpl implements IRolePermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public boolean delectRolePermission(String roleId, String perId) {

        RolePermissionExample example = new RolePermissionExample();
        RolePermissionExample.Criteria criteria = example.createCriteria();

        if(StringUtils.isNotBlank(roleId)){
            criteria.andRoleIdEqualTo(roleId);
        }
        if(StringUtils.isNotBlank(perId)){
            criteria.andPerIdEqualTo(perId);
        }

        if(criteria.isValid()){
            return rolePermissionMapper.deleteByExample(example) > 0;
        }

        return false;

    }

    @Override
    public boolean addRolePermission(String roleId, String perId) {
        RolePermission rolePermission = new RolePermission();
        rolePermission.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        rolePermission.setPerId(perId);
        rolePermission.setRoleId(roleId);
        return rolePermissionMapper.insertSelective(rolePermission) > 0;
    }
}
